/**
 * 语音选择修复测试工具
 * 在浏览器控制台中使用
 */

// 语音选择修复测试工具
window.voiceFixTest = {
    // 测试语音选择是否生效
    async testVoiceSelection() {
        console.log('🔧 测试语音选择修复...');

        const testVoices = [
            { id: 'xiaoyan', name: '小燕', text: '你好，我是小燕，温柔女声。' },
            { id: 'aisjiuxu', name: '艾斯', text: '你好，我是艾斯，御姐音。' },
            { id: 'xiaorong_sichuan', name: '四川话', text: '你好，我是四川话，这是我的声音效果。' },
            { id: 'xiaokun_henan', name: '河南话', text: '你好，我是河南话，这是我的声音效果。' }
        ];

        for (let i = 0; i < testVoices.length; i++) {
            const voice = testVoices[i];
            console.log(`🎤 测试 ${voice.name} (${i + 1}/${testVoices.length})`);

            try {
                if (window.voiceChatManager) {
                    // 选择语音
                    window.voiceChatManager.setVoiceCharacter(voice.id);

                    // 检查TTS参数是否正确更新
                    const params = window.voiceChatManager.ttsParams;
                    console.log(`📋 ${voice.name} TTS参数:`, params);

                    // 停止当前播放
                    window.voiceChatManager.stopSpeaking();

                    // 等待一下
                    await new Promise(resolve => setTimeout(resolve, 500));

                    // 测试语音
                    await window.voiceChatManager.testVoice(voice.text);

                    console.log(`✅ ${voice.name} 测试完成`);
                } else {
                    console.error('❌ voiceChatManager不存在');
                }

                // 等待播放完成
                await new Promise(resolve => setTimeout(resolve, 3000));

            } catch (error) {
                console.error(`❌ ${voice.name} 测试失败:`, error);
            }
        }

        console.log('🔧 语音选择修复测试完成');
    },

    // 检查当前语音设置
    checkCurrentVoice() {
        console.log('🔍 检查当前语音设置...');

        if (window.voiceChatManager) {
            const currentCharacter = window.voiceChatManager.getCurrentVoiceCharacter();
            if (currentCharacter) {
                console.log('📋 当前语音角色:');
                console.log('  - ID:', currentCharacter.id);
                console.log('  - 名称:', currentCharacter.name);
                console.log('  - 描述:', currentCharacter.description);
                console.log('  - VCN:', currentCharacter.vcn);
                console.log('  - 分类:', currentCharacter.category);
            } else {
                console.log('❌ 未设置语音角色');
            }

            const params = window.voiceChatManager.ttsParams;
            console.log('📋 当前TTS参数:');
            console.log('  - vcn:', params.vcn);
            console.log('  - speed:', params.speed);
            console.log('  - volume:', params.volume);
            console.log('  - pitch:', params.pitch);
        } else {
            console.error('❌ voiceChatManager不存在');
        }
    },

    // 列出所有可用语音
    listAllVoices() {
        console.log('📋 所有可用语音:');

        if (window.voiceChatManager) {
            const voices = window.voiceChatManager.getAvailableVoiceCharacters();
            voices.forEach((voice, index) => {
                console.log(`${index + 1}. ${voice.name} (${voice.id}) - ${voice.description} [${voice.category}]`);
            });
        } else {
            console.error('❌ voiceChatManager不存在');
        }
    },

    // 测试单个语音
    async testSingleVoice(voiceId, text) {
        console.log(`🎤 测试语音: ${voiceId}`);

        try {
            if (window.voiceChatManager) {
                // 选择语音
                window.voiceChatManager.setVoiceCharacter(voiceId);

                // 检查TTS参数
                const params = window.voiceChatManager.ttsParams;
                console.log(`📋 TTS参数:`, params);

                // 停止当前播放
                window.voiceChatManager.stopSpeaking();

                // 等待一下
                await new Promise(resolve => setTimeout(resolve, 500));

                // 测试语音
                await window.voiceChatManager.testVoice(text);

                console.log(`✅ ${voiceId} 测试完成`);
            } else {
                console.error('❌ voiceChatManager不存在');
            }
        } catch (error) {
            console.error(`❌ ${voiceId} 测试失败:`, error);
        }
    }
};

// 快速测试函数
window.testVoiceFix = async function () {
    console.log('🔧 快速测试语音选择修复...');
    await window.voiceFixTest.testVoiceSelection();
};

window.checkVoiceFix = function () {
    console.log('🔍 检查语音设置修复...');
    window.voiceFixTest.checkCurrentVoice();
};

window.listVoicesFix = function () {
    console.log('📋 列出所有语音修复...');
    window.voiceFixTest.listAllVoices();
};

window.testSingleFix = async function (voiceId, text) {
    console.log(`🎤 快速测试单个语音修复: ${voiceId}`);
    await window.voiceFixTest.testSingleVoice(voiceId, text);
};

console.log('🔧 语音选择修复测试工具已加载！');
console.log('📖 使用方法:');
console.log('  - testVoiceFix() - 测试所有语音选择修复');
console.log('  - checkVoiceFix() - 检查当前语音设置');
console.log('  - listVoicesFix() - 列出所有可用语音');
console.log('  - testSingleFix("xiaoyan", "你好") - 测试指定语音');
